package com.haogu.service.impl;

import com.haogu.entity.DictType;
import com.haogu.entity.User;
import com.haogu.mapper.DictTypeMapper;
import com.haogu.service.DictTypeService;
import org.springframework.stereotype.Service;
import org.springframework.ui.Model;

import javax.annotation.Resource;
import javax.servlet.http.HttpSession;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
@Service
public class DictTypeServiceImpl implements DictTypeService {
    @Resource
    private DictTypeMapper dictTypeMapper;
    /**
     * 添加字典类型
     * @param dictType
     * @param session
     * @return
     */
    @Override
    public Map addDictType(DictType dictType, HttpSession session) {
        User user = (User) session.getAttribute("user");
        dictType.setCreateBy(user.getId());
        int insert = dictTypeMapper.insert(dictType);
        Map map = new HashMap();
        if(insert == 0){
            map.put("success",false);
        }else{
            map.put("success",true);
        }
        return map;
    }
    /**
     * 分页展示字典类型数据
     */
    public Map selectDictTypeByPage(){
        List<DictType> dictTypes = dictTypeMapper.selectDictTypeByPage();
        List<DictType> dictTypes1 = dictTypeMapper.selectList(null);
        Map map = new HashMap();
        map.put("data",dictTypes1);

        return map;
    }
    /**
     * 去修改页面之前的回显
     */
    public void toUpdateDictType(Integer id, Model model){
        System.out.println(id+"111111111111111111111");
        DictType dictType = dictTypeMapper.selectById(id);
        model.addAttribute("dictType",dictType);
    }
    /**
     * 修改DictType
     */
    public Map updateDictType(DictType dictType,User user){
        dictType.setUpdateBy(user.getId());
        Map map = new HashMap();
        int i = dictTypeMapper.updateById(dictType);
        if(i==0){
            map.put("success",false);
        }else{
            map.put("success",true);
        }
        return map;
    }
    /**
     * 删除DictType
     */
    public Map delectDictType(Integer id){
        Map map = new HashMap();
        int i = dictTypeMapper.deleteById(id);
        if(i==0){
            map.put("success",false);
            map.put("message","删除失败");
        }else{
            map.put("success",true);
            map.put("message","删除成功");
        }
        return map;
    }

}
